<?php


namespace app\plugin\controller\merchant;


use app\common\controller\AppCommon;
use app\service\CommonUser;
use app\service\ConfigService;
use app\service\PluginsService;
use app\service\VerifyCode;
use think\Controller;
use think\Request;

class Account extends Controller
{
    public $tag = 'merchant';
    public $param = null;


    public $plugin = null;
    private $m_uid = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (!bs_p($this->tag)){
            if (IS_AJAX){
                data_return('应用已关闭',-1);
            }
            return $this->error('应用已关闭');
        }

        $this->m_uid = session('my_merchant_uid','','bs_merchant_');

        $this->param = input();


    }

    public function reg()
    {
        return  $this->fetch();
    }

    public function reg_action()
    {
        //todo 配置动态化
       /* $conf = ConfigService::get('mobile_shop');
        if (empty($conf['reg_type']) || $conf['reg_type'] == -1) {
            data_return('系统已关闭注册', -1);
        }*/
        $rule = [
            ['type' => 'length', 'key' => 'account', 'rule' => '2,32', 'msg' => '账号2~32字符',],
            ['type' => 'empty', 'key' => 'pwd1', 'rule' => '', 'msg' => '密码不能为空',],
            ['type' => 'empty', 'key' => 'pwd2', 'rule' => '', 'msg' => '确认密码不能为空',],
        ];
        $check = check_param($this->param, $rule);
        if ($check['code'] <> 0) {
            data_return($check['msg'], $check['code']);
        }
        if (strlen($this->param['account']) < 6) {
            data_return('账号不合法', -1);
        }
        if (strlen($this->param['pwd1']) < 6) {
            data_return('密码不够安全', -1);
        }

        if ($this->param['pwd1'] !== $this->param['pwd2']) {
            data_return('两次密码不一样', -1);
        }

        $has = AppCommon::data_get('plugin_merchant', ['account' => trim($this->param['account'])], 'id');
        if (!empty($has['id'])) {
            data_return('账号已被注册', -1);
        }

        $salt = get_random(10, false);
        $uid = 'bs' . md5($this->param['account'] . rand(1, 99999) . time() . $salt);
        while (true) {
            $tmp = AppCommon::data_get('plugin_merchant', ['uid' => $uid], 'id');
            if (empty($tmp['id'])) {
                break;
            }
            $uid = 'bs' . md5($this->param['account'] . rand(1, 99999) . time() . get_random(10, false));
        }
        $data = [
            'user_name' => '商家-'.rand(10000,1000000),
            'account' => trim($this->param['account']),
            'uid' => $uid,
            'pwd' => md5($this->param['pwd1'] . $salt),
            'salt' => $salt,
            'add_time' => time(),
            'up_time' => time(),
            //0-禁用，1-启用，-1-待审核，-2-已驳回
            'status' => -1,
            'loginCount' => 1,
        ];

       AppCommon::data_add('plugin_merchant',$data);
        $loginData = [
            'uid' => $data['uid'],
            'add_time' => time(),
            'ip' => get_ip()
        ];
        AppCommon::data_add('plugin_merchant_login_log', $loginData);

        session('my_merchant_uid', $data['uid'], 'bs_merchant_');

        data_return('注册成功', 0,['url'=>url('/plugin/'.$this->tag.'.manager/index')]);
    }

    public function login()
    {
        if ($this->m_uid){
            return $this->redirect(url('/plugin/'.$this->tag.'.manager/index'));
        }
        return  $this->fetch();
    }

    public function login_action()
    {
        if (empty($this->param['account'])) {
            data_return('账号未设置', -1);
        } elseif (empty($this->param['pwd'])) {
            data_return('密码未输入', -1);
        }
        $data = AppCommon::data_get('plugin_merchant', ['account' => trim($this->param['account'])], 'id,uid,pwd,salt,pwd_err_count,up_time,loginCount');

        if (empty($data)) {
            data_return('账号不存在', -1);
        }
        if ($data['pwd_err_count'] >= 5 && ($data['up_time'] > time() - 1800)) {
            data_return('密码错误次数过多，请稍后重试', -1);
        }
        if (md5($this->param['pwd'] . $data['salt']) <> $data['pwd']) {
            AppCommon::data_update('plugin_merchant', ['id' => $data['id']], ['pwd_err_count' => $data['pwd_err_count'] + 1, 'up_time' => time()]);
            data_return('账号密码不匹配', -1);
        }
        //重生盐值加密
        $salt = get_random(10, false);
        $pwd = md5($this->param['pwd'] . $salt);
        AppCommon::data_update('plugin_merchant', ['id' => $data['id']],
            ['pwd_err_count' => 0, 'salt' => $salt, 'pwd' => $pwd, 'loginCount' => $data['loginCount'] + 1]);

        $loginData = [
            'uid' => $data['uid'],
            'add_time' => time(),
            'ip' => get_ip()
        ];
        AppCommon::data_add('plugin_merchant_login_log', $loginData);


        session('my_merchant_uid', $data['uid'], 'bs_merchant_');

        //登录成功返回的链接
        $backUrl = cookie('my_merchant_backurl');
        if (!$backUrl) {
            $backUrl = url('/plugin/'.$this->tag.'.manager/index');
        } else {
            cookie('my_merchant_backurl', null);
        }
        data_return('登录成功', 0, ['url' => $backUrl]);
    }


    //退出登录
    public function logout()
    {
        session('my_merchant_uid', null, 'bs_merchant_');
        return $this->redirect(url('login'));
    }
}